
package com.jf.cloud.platform.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.auth.feign.AccountFeignClient;
import com.jf.cloud.api.auth.vo.AuthAccountVO;
import com.jf.cloud.api.platform.vo.SysUserVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.platform.dto.ChangeAccountDTO;
import com.jf.cloud.platform.service.SysUserAccountService;
import com.jf.cloud.platform.service.SysUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zz
 * @date 2020/12/21
 */
@RequestMapping(value = "/p/sys_user/account")
@RestController
@Tag(name = "平台用户账号信息")
public class SysUserAccountController {

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    private final SysUserAccountService SysUserAccountService;

    private final SysUserService SysUserService;

    private final AccountFeignClient accountFeignClient;

    public SysUserAccountController(SysUserAccountService sysUserAccountService, SysUserService sysUserService, AccountFeignClient accountFeignClient) {
        this.SysUserAccountService = sysUserAccountService;
        this.SysUserService = sysUserService;
        this.accountFeignClient = accountFeignClient;
    }


    @GetMapping
    @Operation(summary = "获取账号信息" , description = "获取账号信息")
    public ServerResponseEntity<AuthAccountVO> getAccount(Long userId) {
        return SysUserAccountService.getByUserIdAndSysType(userId, AuthUserContext.get().getSysType());
    }


    @PostMapping
    @Operation(summary = "添加账号" , description = "添加账号")
    public ServerResponseEntity<Void> addAccount(@Valid @RequestBody ChangeAccountDTO changeAccountDTO) {
        SysUserVO sysUserVO = SysUserService.getByUserId(changeAccountDTO.getUserId());
        if (sysUserVO == null) {
            return ServerResponseEntity.showFailMsg("无法获取账户信息");
        }
        if (Objects.equals(sysUserVO.getHasAccount(), 1)) {
            return ServerResponseEntity.showFailMsg("已有账号，无需重复添加");
        }
        if (StrUtil.isBlank(changeAccountDTO.getPassword())) {
            return ServerResponseEntity.showFailMsg("密码不能为空");
        }
        return SysUserAccountService.save(changeAccountDTO);
    }

    @PutMapping
    @Operation(summary = "修改账号" , description = "修改账号")
    public ServerResponseEntity<Void> updateAccount(@Valid @RequestBody ChangeAccountDTO changeAccountDTO) {
        if (Objects.equals(changeAccountDTO.getUserId().intValue(), Constant.SUPER_ADMIN_ID) && BooleanUtil.isFalse(permission)) {
            throw new LuckException("暂无权限修改该用户的账户信息");
        }
        SysUserVO sysUserVO = SysUserService.getByUserId(changeAccountDTO.getUserId());
        if (sysUserVO == null || Objects.equals(sysUserVO.getHasAccount(), 0)) {
            return ServerResponseEntity.showFailMsg("无法获取账户信息");
        }
        ServerResponseEntity<AuthAccountVO> authAccountEntity = accountFeignClient.getByUserIdAndType(sysUserVO.getSysUserId(), SysTypeEnum.PLATFORM.value());
        if (authAccountEntity.isFail()) {
            throw new LuckException(authAccountEntity.getMsg());
        }
        AuthAccountVO authAccountVO = authAccountEntity.getData();
        if (Objects.equals(authAccountVO.getIsAdmin(), 1) && !Objects.equals(sysUserVO.getSysUserId(), AuthUserContext.get().getUserId())) {
            return ServerResponseEntity.showFailMsg("您没有权限修改管理员信息");
        }
        return SysUserAccountService.update(changeAccountDTO);
    }
}
